//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.model.vo
{
	import __AS3__.vec.Vector;
	
	public class BlogSourceVO extends SelectAbleVO
	{
		//----------------------------------------------------------------------------
		//
		// porperties
		//
		//----------------------------------------------------------------------------
		
		[Bindable] public var blogName		:String;
		[Bindable] public var blogLink		:String;
		[Bindable] public var blogRSS		:String;
		public var blogSourceId	:int;
		public var labelId		:int = -1;
		
		public function get label():String
		{
			return blogName.length > 12 ? blogName.substr(0, 10) + "..." : blogName;
		}
		
		//lastPubDate
		private var _lastPubDate:Date;
		public function get lastPubDate():Date
		{
			return _lastPubDate ? _lastPubDate : createLastPubDate();
		}		
		public function set lastPubDate(value:Date):void
		{
			this._lastPubDate = value;
		}
		
		//total blogs
		private var _totalBlogs	:int;
		[Bindable]
		public function get totalBlogs():int
		{
			return _totalBlogs < 0 ? 0 : _totalBlogs;
		}		
		public function set totalBlogs(value:int):void
		{
			this._totalBlogs = value < 0 ? 0 : value;
		}
		
		////total blogs
		private var _newBlogs	:int;
		[Bindable]
		public function get newBlogs():int
		{
			return _newBlogs < 0 ? 0 : _newBlogs;
		}
		public function set newBlogs(value:int):void
		{
			this._newBlogs = value < 0 ? 0 : value;
		}
		
		public var weight:int;
		public var isRefreshing:Boolean;
		
		public function toString():String
		{
			return "[BlogSource] blogSourceId=" + blogSourceId + " blogName=" + blogName + "\n";
		}
		
		public static function createLastPubDate():Date
		{
			var ld:Number = new Date().getTime();
			return new Date(ld - 604800000);
		}
		
		//----------------------------------------------------------------------------
		//
		// filter methods
		//
		//----------------------------------------------------------------------------
		
		public static function getBlogSourceById(blogSourceId:int, blogSourceArray:Vector.<BlogSourceVO>):BlogSourceVO
		{
			var r:BlogSourceVO;
			for each(var bs:BlogSourceVO in blogSourceArray)
			{
				if(bs.blogSourceId == blogSourceId)
					r = bs;
			}
			return r;
		}
		
		public static function removeBlogSourceById(blogSourceId:int, blogSourceArray:Vector.<BlogSourceVO>):void
		{
			for(var i:int = 0; i < blogSourceArray.length; i++)
			{
				if(blogSourceArray[i].blogSourceId == blogSourceId)
				{
					blogSourceArray.splice(i, 1);
					return;
				}
			}
		}
		
		public static function getBlogSourceIdsByLabel(label:BlogLabelVO, blogSourceArray:Vector.<BlogSourceVO>):Vector.<int>
		{
			var r:Vector.<int> = new Vector.<int>();
			for each(var bs:BlogSourceVO in blogSourceArray)
			{
				if(bs.labelId == label.labelid)
					r.push(bs.blogSourceId);
			}
			return r;
		}
		
		public static function getNewBlogsBlogSource(blogSourceArray:Vector.<BlogSourceVO>):Vector.<BlogSourceVO>
		{
			var a:Vector.<BlogSourceVO> = new Vector.<BlogSourceVO>();
			for each(var bs:BlogSourceVO in blogSourceArray)
			{
				if(bs.newBlogs > 0)
				{
					a.push(bs);
				}
			}
			return a;
				
		}
		
		//----------------------------------------------------------------------------
		//
		// Convert Vector to Array
		//
		//----------------------------------------------------------------------------
		
		public static function VectorToArray(v:Vector.<BlogSourceVO>):Array/* of BlogSourceVO */
		{
			var a:Array = [];
			for each(var i:BlogSourceVO in v)
			{
				a.push(i);
			}
			return a;
		}
		
		public static function ArrayToVector(a:Array/* of BlogSourceVO */):Vector.<BlogSourceVO>
		{
			var v:Vector.<BlogSourceVO> = new Vector.<BlogSourceVO>();
			for each(var i:BlogSourceVO in a)
			{
				v.push(i);
			}
			return v;
		}
	}
}